package com.bwie.a0522_chenzihang.Adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwie.a0522_chenzihang.BookBean;
import com.bwie.a0522_chenzihang.DBHelper.OpenHelper;
import com.bwie.a0522_chenzihang.R;

import java.util.List;

public class ListViewAdapter extends BaseAdapter {
    Context context;
    List<BookBean> bookBeanList;
    SQLiteDatabase db;
    OpenHelper helper;

    public ListViewAdapter(Context context, List<BookBean> bookBeanList, SQLiteDatabase db, OpenHelper helper) {
        this.context = context;
        this.bookBeanList = bookBeanList;
        this.db = db;
        this.helper = helper;
    }

    @Override
    public int getCount() {
        return bookBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return bookBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.item_book_lv,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        final BookBean bookBean = bookBeanList.get(position);
        holder.name.setText(bookBean.getName());
        holder.author.setText(bookBean.getAuthor());
        holder.count.setText(bookBean.getCount());
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示信息").setMessage("您确定舍弃更改吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                bookBeanList.remove(bookBean);
                                db=helper.getWritableDatabase();
                                db.delete("book","name=?",new String[]{bookBean.getName()});
                                notifyDataSetChanged();

                            }
                        });
                builder.setNegativeButton("取消",null);
                builder.create().show();
            }
        });
        return convertView;
    }
    class ViewHolder{
        TextView name,author,count;
        ImageView bianji,delete;
        public ViewHolder(View view){
            name=view.findViewById(R.id.book_name);
            author=view.findViewById(R.id.author_name);
            count=view.findViewById(R.id.count);
            bianji=view.findViewById(R.id.book_iv_bianji);
            delete=view.findViewById(R.id.book_iv_delete);
        }
    }
}